package top.jiangqiang.qianyi.business.service.impl;

import org.dromara.hutool.core.text.StrUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import top.jiangqiang.qianyi.business.model.request.SysLogListRequest;
import top.jiangqiang.qianyi.business.model.request.SysLogPageRequest;
import top.jiangqiang.qianyi.business.model.response.SysLogDetailResponse;
import top.jiangqiang.qianyi.business.model.response.SysLogListResponse;
import top.jiangqiang.qianyi.business.model.response.SysLogPageResponse;
import top.jiangqiang.qianyi.business.service.SysLogService;
import top.jiangqiang.qianyi.common.base.model.response.PageResult;
import top.jiangqiang.qianyi.common.base.model.response.Result;
import top.jiangqiang.qianyi.common.base.service.impl.BaseServiceImpl;
import top.jiangqiang.qianyi.common.log.model.SysLogSaveRequest;
import top.jiangqiang.qianyi.data.model.entity.SysLog;
import top.jiangqiang.qianyi.data.service.SysLogDbService;

import java.util.List;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author JiangQiang
 * @since 2024-06-03
 */
@Service
public class SysLogServiceImpl extends BaseServiceImpl<SysLogDbService, SysLog> implements SysLogService {

    @Override
    public Result<PageResult<SysLogPageResponse>> page(SysLogPageRequest request) {
        PageResult<SysLogPageResponse> page = request.toPage();
        return Result.ok(getBaseDbService().lambdaJoinWrapper().page(page, SysLogPageResponse.class));
    }

    @Override
    public Result<List<SysLogListResponse>> list(SysLogListRequest request) {
        return Result.ok(getBaseDbService().lambdaJoinWrapper().list(SysLogListResponse.class));
    }

    @Override
    public Result<SysLogDetailResponse> detail(Long id) {
        return Result.ok(getById(id, SysLogDetailResponse.class));
    }

    @Override
    public Result<Void> delete(Long id) {
        removeById(id);
        return Result.ok();
    }

    /**
     * 只保存日志名不为空的日志，日志名不为空说明该日志对应的路径上存在日志注解，并且注解的value不为空
     * 此注解只可能出现在接口上，而非静态资源相关的请求
     *
     * @param logRequest 日志数据信息
     */
    @Override
    @Async
    public void saveLog(SysLogSaveRequest logRequest) {
        SysLog sysLog = typeConverter.convert(logRequest, SysLog.class);
        if (StrUtil.isNotBlank(sysLog.getLogName())) {
            save(sysLog);
        }
    }
}